combination object

This algorithm generates all unique combinations of a given size from a set.

bool generate_unique_combinations(int items, int combination_size)

Parameters:
items
The number of items in the set. This must be at least 3.
combination_size
The number of items in each combination. Since the generated combinations must be unique, this value must be greater than 1 and less than items.

Return value:
true on success, false on failure.

Remarks:
When enumerating combinations with two items in each from a set of four items, the six possible combinations that this algorithm generates are:

[ 0, 1 ]
[ 0, 2 ]
[ 0, 3 ]
[ 1, 2 ]
[ 1, 3 ]
[ 2, 3 ]

Example:
// We have 4 sports teams and we want each of them to play against every other team once, but no more than that.

void main()
{
int combination_size=2;
// This is the number of items that we want in each combination.

int items=4;
// This is the number of items we have all in total, which is to say our sports teams.

combination process;

if(process.generate_unique_combinations(items, combination_size)==false)
{
alert("Error", get_last_error_text());
exit();
}

int[] list;
// This is the array which will be filled with each combination.

int count=0;
// This simply keeps track of how many combinations we've generated so far. It is not really needed.

while(process.next(list))
{
count+=1;

// At this point, we have a new combination. We print it out in an alert box as a comma separated string.
string data;
for(int i=0;i<list.length();i++)
{
if(i>0)
data+=", ";
data+=list[i];
}
alert("Combination " + count, data);
}

// We're done, so let's display how many combinations we found before we quit.
alert("Result", count + " unique combinations were generated from a set of " + items + " items, with " + combination_size + " items in each combination.");
}